//
//  MovieViewController.m
//  UI_元旦豆瓣
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 31付冬梅. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieModel.h"
#import "MovieView.h"
#import "UIImageView+WebCache.h"
#import "MovieTableViewCell.h"
#import "MovieViewController3.h"
#import "MovieViewController2.h"
#import "AfraDataBase2016010902Movie.h"
#define kWidth self.view.frame.size.width
#define kHeitht self.view.frame.size.height
@interface MovieViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *movieArr;
@property (nonatomic, retain) NSMutableArray *modelArr;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation MovieViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];
    self.title = @"电影";
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item1;
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(itemAction2:)];
//    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction3:)];
    self.navigationItem.leftBarButtonItem = item2;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //数据处理
        NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
        self.movieArr = [NSMutableArray arrayWithContentsOfFile:path];
        self.modelArr = [NSMutableArray array];
        for (NSDictionary *dic in _movieArr) {
            MovieModel *model = [MovieModel movieModel];
            [model setValuesForKeysWithDictionary:dic];
            [_modelArr addObject:model];
        }
    
    [self.tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"movieCell"];
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (void)itemAction3:(UIBarButtonItem *)item3 {
//    MovieModel *model = [MovieModel movieModel];
//    model.title = self.title;
//    model.image = self.im
//    [[AfraDataBase2016010902Movie sharedDataBaseMovieHandle] insertMovieModel:model];
}
- (void)itemAction2:(UIBarButtonItem *)item2 {
    MovieViewController3 *view3 = [[MovieViewController3 alloc] init];
    
    [self.navigationController pushViewController:view3 animated:YES];
}
- (void)itemAction:(UIBarButtonItem *)item {
    MovieViewController2 *view2 = [[MovieViewController2 alloc] init];
    
    [self.navigationController pushViewController:view2 animated:YES];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _movieArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieCell"];
    MovieModel *model = _modelArr[indexPath.row];
    cell.titleLabel.text = model.title;
    [cell.movieImageView sd_setImageWithURL:[NSURL URLWithString:model.image]];
    return cell;
    
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
////    MovieDetailViewController *detail = [[MovieDetailViewController alloc] init];
////    MovieModel *model = _modelArr[indexPath.row];
////    detail.movieModelArray = _modelArr;
////    detail.titleStr = model.title;
////    [self.navigationController pushViewController:detail animated:YES];
//    
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
